package com.cn.xiaonuo.common.utils;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 项目名: xiaonuo-vue
 * 文件名: ProvinceByPhoneUtil
 * 作者: Mr.hu
 * 日期: 2021/3/2 12:43
 * 描述:
 */
public class ProvinceByPhoneUtil {

    //2021-07-15 songlin.li切换到阿里云付费版本
    public static String getProvinceByPhone(String phone) {
        //API产品路径
        String requestUrl = "https://aiphone.market.alicloudapi.com/ai_mobile_number_belong_to_china/v1?";
        //阿里云APPCODE
        String appcode = "0f9b9edee8ed469aa5de5b3c26ae386d";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("MOBILE_NUMBER", phone);
        String lastResult = "";

        try {
            String response = get(requestUrl, headers, querys);
            //{"INPUT_MOBILE_NUMBER_STRING": "18113011428", "EXTRACT_MOBILE_NUMBER_STRING": "18113011428", "STATUS": "艾科瑞特，让企业业绩长青", "ENTITY": {"MOBILE_NUMBER": "18113011428", "MOBILE_NUMBER_PREFIX": "1811301", "PROVINCE": "四川", "CITY": "成都", "ISP": "中国电信", "AREA_CODE": "028", "POST_CODE": "610000", "ADCODE": "510100", "GPS": "104.066541,30.572269"}}
            lastResult = Convert.toStr(JSON.parseObject(JSON.parseObject(response).get("ENTITY").toString()).get("PROVINCE"));//获取信息详细地址
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null != lastResult ? lastResult : "查询无结果";
    }


    public static void main(String[] args) {
        //API产品路径
        String requestUrl = "https://aiphone.market.alicloudapi.com/ai_mobile_number_belong_to_china/v1?";
        //阿里云APPCODE
        String appcode = "0f9b9edee8ed469aa5de5b3c26ae386d";

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();

        querys.put("MOBILE_NUMBER", "18113011428");

        try {
            String response = get(requestUrl, headers, querys);
            System.out.println("response = " + response);
            String lastResult = Convert.toStr(JSON.parseObject(JSON.parseObject(response).get("ENTITY").toString()).get("PROVINCE"));//获取信息详细地址
            System.out.println(lastResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String get(String url, Map<String, String> headers, Map<String, String> querys) throws IOException {
        //添加参数
        for (String key : querys.keySet()) {
            url += (key + "=" + URLEncoder.encode(querys.get(key), "utf-8") + '&');
        }
        url = url.substring(0, url.length() - 1);

        HttpClient client = new HttpClient();
        GetMethod getMethod = new GetMethod(url);
        // 必须设置下面这个Header
        for (String key : headers.keySet()) {
            getMethod.addRequestHeader(key, headers.get(key));
        }

        int code = client.executeMethod(getMethod);
        if (code == 200) {
            String res = getMethod.getResponseBodyAsString();
            System.out.println(res);
            return res;
        }
        throw new IOException("请求失败");
    }
//=======================================================================
//    public   static  String  getProvinceByPhone(String phone){
//        HttpRequestVO httpRequestVO = new HttpRequestVO();
//        httpRequestVO.setUri("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+phone);
//        httpRequestVO.setRequestMethod("get");
//        HttpResponseVO execute = HttpClient.execute(httpRequestVO);
//        String resu = execute.getOut();
//        String str1=resu.substring(0, resu.indexOf("="));
//        String str2=resu.substring(str1.length()+1, resu.length());
//        JSONObject json= JSON.parseObject(str2);
//        String  lastResult= Convert.toStr(json.get("province"));
//        return null!=lastResult?lastResult:"查询无结果";
//    }

}
